﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DomainDriver.DomainModeling.Constraints;
using DomainDriver.DomainModeling.DataProviders;
using DomainDriver.DomainModeling.DomainModels;

namespace DomainDriver.DomainModeling.GenericUnitTesting.Constraints
{
    // NOTE: Add "[TestFixture]" to sub-classes in actual test assembly

    public abstract class ConstraintTestFixture<DOMAIN_MODEL, CONSTRAINT> : DomainModelTestFixture<DOMAIN_MODEL>
        where DOMAIN_MODEL : class, IDomainModel
        where CONSTRAINT : class, IConstraint
    {
        private bool m_UsesConstraintName = false;
        private string m_ConstraintName = null;

        public ConstraintTestFixture(DataSourcePersistenceType persistenceType)
            : base(persistenceType, true, true)
        {
            m_UsesConstraintName = false;
            m_ConstraintName = null;
        }

        public ConstraintTestFixture(DataSourcePersistenceType persistenceType, string constraintName)
            : base(persistenceType, true, true)
        {
            m_UsesConstraintName = true;
            m_ConstraintName = constraintName;
        }

        public bool UsesConstraintName
        { get { return m_UsesConstraintName; } }

        public string ConstraintName
        { get { return m_ConstraintName; } }

        public CONSTRAINT GetConstraintToTest()
        {
            IConstraint constraintToTest = null;
            if (!UsesConstraintName)
            { constraintToTest = DomainModel.GetConstraint<CONSTRAINT>(); }
            else
            { constraintToTest = DomainModel.GetConstraint<CONSTRAINT>(ConstraintName); }
            return constraintToTest as CONSTRAINT;
        }

        [SetUp]
        public override void SetUp()
        {
            base.SetUp();

            foreach (IConstraint constraint in DomainModel.Constraints)
            {
                constraint.Enabled = false;
            }

            IConstraint constraintToTest = GetConstraintToTest();
            constraintToTest.Enabled = true;
        }
    }
}